package easy;

import java.util.ArrayList;
import java.util.List;
import util.PrintUtils;

public class Solution_1389 {

    public int[] createTargetArray(int[] nums, int[] index) {
        boolean[] flags = new boolean[nums.length];
        int[] res = new int[nums.length];

        for (int i = 0; i < index.length; i++) {
            if (!flags[index[i]]) {
                flags[index[i]] = true;
            } else {
                int pre = res[index[i]];
                for (int j = index[i]+1; j < res.length; j++) {
                    if (flags[j]) {
                        int temp = pre;
                        pre = res[j];
                        res[j] = temp;
                    } else {
                        res[j] = pre;
                        flags[j] = true;
                    }
                }
            }
            res[index[i]] = nums[i];
        }
        return res;
    }

    public static void main(String[] args) {
        Solution_1389 model = new Solution_1389();
        PrintUtils.arrayInt(model.createTargetArray(new int[]{4, 2, 4, 3, 2}, new int[]{0, 0, 1, 3, 1}));
        PrintUtils.arrayInt(model.createTargetArray(new int[]{0, 1, 2, 3, 4}, new int[]{0, 1, 2, 2, 1}));
        PrintUtils.arrayInt(model.createTargetArray(new int[]{1, 2, 3, 4, 0}, new int[]{0, 1, 2, 3, 0}));
        PrintUtils.arrayInt(model.createTargetArray(new int[]{1}, new int[]{0}));
    }
}
